Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

cors

Package Overview
Dependencies
Maintainers
2
Versions
34
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

cors

Node.js CORS middleware

  • 2.8.5
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
14M
increased by8.86%
Maintainers
2
Weekly downloads
 
Created

What is cors?

The cors npm package is a middleware for enabling Cross-Origin Resource Sharing (CORS) in web applications using Node.js and Express. It allows you to specify which domains can access resources on your server, which HTTP methods are allowed, and other security-related configurations to prevent or allow cross-site requests.

What are cors's main functionalities?

Basic Usage

This code sample demonstrates the basic usage of the cors package where it enables CORS for all routes and origins by default.

const express = require('express');
const cors = require('cors');

const app = express();

app.use(cors());

app.get('/data', (req, res) => {
  res.json({ message: 'This route is CORS-enabled for all origins!' });
});

app.listen(3000, () => {
  console.log('Server running on port 3000');
});

Configuring CORS for Specific Origins

This code sample shows how to configure CORS to allow requests from a specific origin, in this case, 'http://example.com'.

const express = require('express');
const cors = require('cors');

const app = express();

const corsOptions = {
  origin: 'http://example.com',
  optionsSuccessStatus: 200
};

app.use(cors(corsOptions));

app.get('/data', (req, res) => {
  res.json({ message: 'This route is CORS-enabled for http://example.com!' });
});

app.listen(3000, () => {
  console.log('Server running on port 3000');
});

Enabling CORS for Dynamic Origins

This code sample illustrates how to enable CORS based on a dynamic list of origins. The origins are checked against a whitelist, and CORS is configured accordingly.

const express = require('express');
const cors = require('cors');

const app = express();

const whitelist = ['http://example1.com', 'http://example2.com'];
const corsOptionsDelegate = function (req, callback) {
  let corsOptions;
  if (whitelist.indexOf(req.header('Origin')) !== -1) {
    corsOptions = { origin: true };
  } else {
    corsOptions = { origin: false };
  }
  callback(null, corsOptions);
};

app.use(cors(corsOptionsDelegate));

app.get('/data', (req, res) => {
  res.json({ message: 'This route has CORS enabled for specific origins!' });
});

app.listen(3000, () => {
  console.log('Server running on port 3000');
});

Other packages similar to cors

Keywords

FAQs

Package last updated on 04 Nov 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc